package com.xyd.Servlet;

import com.xyd.Biz.Impl.NewsBizImpl;
import com.xyd.Biz.NewsBiz;
import com.xyd.Entity.News;
import com.xyd.Util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/selNewsServlet")
public class selNewsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op =  req.getParameter("op");
        if(op.equals("list")){
            getlist(req, resp);
        }else if(op.equals("byid")){
            getbyid(req, resp);
        }else if(op.equals("selAll")){
            getselAll(req, resp);
        }else if(op.equals("tannouncementsList")){
            tannouncementsList(req, resp);
        }else if(op.equals("newsList")){
            newsList(req, resp);
        }
    }

    public void getlist(HttpServletRequest req, HttpServletResponse resp){
        NewsBiz newsBiz = new NewsBizImpl();
        List<News> tannouncementsList = newsBiz.selectannouncements(0);
        List<News> newsList = newsBiz.selectNews(0);
        Map<String,Object> map =new HashMap<>();
        map.put("tannouncementsList",tannouncementsList);
        map.put("newsList",newsList);
        JsonServlet.getJson(req,resp,map);
    }

    public void getbyid(HttpServletRequest req, HttpServletResponse resp){
        NewsBiz newsBiz = new NewsBizImpl();
        String id = req.getParameter("id");
        News news = newsBiz.selectbyId(Integer.parseInt(id));
        Map<String,Object> map =new HashMap<>();
        map.put("news",news);
        JsonServlet.getJson(req,resp,map);
    }

    public void getselAll(HttpServletRequest req, HttpServletResponse resp){
        NewsBiz newsBiz = new NewsBizImpl();
        List<News> List = newsBiz.selectAllNews();
        Map<String,Object> map =new HashMap<>();
        map.put("List",List);
        JsonServlet.getJson(req,resp,map);
    }
    public void tannouncementsList(HttpServletRequest req, HttpServletResponse resp){
        NewsBiz newsBiz = new NewsBizImpl();
        int pagenum = Integer.parseInt(req.getParameter("pagenum"));
        int pagemax = newsBiz.selecttypecount(0);
        pagemax = pagemax%7==0?pagemax/7:pagemax/7+1;
        pagenum = pagenum<1?1:pagenum;
        pagenum = pagenum>pagemax?pagemax:pagenum;
        List<News> tannouncementsList = newsBiz.selectannouncements((pagenum-1)*7);

        Map<String,Object> map =new HashMap<>();
        map.put("tannouncementsList",tannouncementsList);
        map.put("num",pagenum);
        JsonServlet.getJson(req,resp,map);
    }
    public void newsList(HttpServletRequest req, HttpServletResponse resp){
        NewsBiz newsBiz = new NewsBizImpl();
        int pagenum = Integer.parseInt(req.getParameter("pagenum"));
        int pagemax = newsBiz.selecttypecount(0);
        pagemax = pagemax%7==0?pagemax/7:pagemax/7+1;
        pagenum = pagenum<1?1:pagenum;
        pagenum = pagenum>pagemax?pagemax:pagenum;
        List<News> newsList = newsBiz.selectNews((pagenum-1)*7);

        Map<String,Object> map =new HashMap<>();
        map.put("newsList",newsList);
        map.put("num",pagenum);
        JsonServlet.getJson(req,resp,map);
    }
}
